package com.chookss.tiku

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.chookss.tiku.entity.LiveResult
import com.chookss.tiku.entity.Subject
import com.chookss.tiku.response.SearchSubjectList
import com.chookss.tools.httpForm

/**
 * Company:
 * Project:
 * Author: liuhaoge
 * Date: 2021/01/05
 * Note:
 */
class SearchSubjectViewModel : BasePracticeTestViewModel() {

    //题目列表
    val subjectList = mutableListOf<Subject>()
    var keyword: String? = null
    override fun submitAnswer(subject: Subject, answers: List<String>, time: Long) = MutableLiveData<LiveResult<Any>>()

    //下载题目列表
    override fun getSubject(index: Int): LiveData<LiveResult<Subject>> = MutableLiveData()

    fun getSubjectList(): LiveData<LiveResult<List<Subject>>> {
        var liveResult = MutableLiveData<LiveResult<List<Subject>>>()

        jobs + httpForm<SearchSubjectList.Response> {
            url = "api/subject/searchSubject"
            params = mutableMapOf("content" to keyword)
            onSuccess = {
                subjectList.clear()
                subjectList.addAll(it.data ?: emptyList())
                liveResult.postValue(LiveResult(subjectList))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }

        return liveResult
    }

}